import dateFormat from "vux/src/tools/date/format.js";

import dataTimeFilter from "../../common/dataTimeFilter/dataTimeFilter";
import { addClass, removeClass } from "@/assets/js/dom.js";
import template from "./bettingRecordTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    dataTimeFilter,
  },
  data() {
    return {
      showList: [],
      nowId: "",
      recordList: [],

      lotteryList: [{ id: 0, name: "全部彩种" }],

      lottery: "",
      lotteryType: "",

      status: "",
      statusId: "",
      type: "",
      statusList: [{ id: 0, name: "全部状态" }],
      pikerShow: false,
      pickerTitle: "彩种类型",
      datePicker: false,
      beginTime: dateFormat(new Date(), "YYYY-MM-DD"),
      endTime: dateFormat(new Date(), "YYYY-MM-DD"),
      page: 1,

      award: "",
      money: "",
      total: "",

      fromPath: "",
    };
  },
  created() {
    this.resetAll(1);
  },
  mounted() {
    this.lotteryType = this.$route.query.lottery_type;
  },
  methods: {
    activeCopy(e) {
      addClass(e.target, "activeScroll");
      document.addEventListener("touchmove", () => {
        removeClass(e.target, "activeScroll");
      });
      document.addEventListener("touchend", () => {
        removeClass(e.target, "activeScroll");
      });
    },
    pullingDown() {
      this.page = 1;
      this.getLottery("down");
    },
    pullingUp() {
      if (this.flag) {
        this.$refs.scroll.forceUpdate(false);
        return;
      }
      this.page += 1;
      this.getLottery("up");
    },
    resetAll(flag) {
      let vm = this;
      vm.status = vm.statusList[0];
      vm.lottery = vm.lotteryList[0];
      if (flag && vm.$route.query.lottery_type) {
        vm.lotteryType = vm.$route.query.lottery_type;
      } else {
        vm.lotteryType = vm.lotteryList[0].id;
      }

      vm.statusId = vm.statusList[0].id;
      vm.value1 = dateFormat(new Date(), "YYYY-MM-DD");
      vm.value2 = dateFormat(new Date(), "YYYY-MM-DD");
      vm.beginTime = dateFormat(new Date(), "YYYY-MM-DD");
      vm.endTime = dateFormat(new Date(), "YYYY-MM-DD");
      vm.getLottery();
    },
    showPicker(type, id) {
      this.type = type;
      if (type == 0) {
        this.showList = this.lotteryList;
        this.pickerTitle = "彩种类型";
      } else if (type == 1) {
        this.showList = this.statusList;
        this.pickerTitle = "交易状态";
      }
      this.pikerShow = true;
      this.nowId = id;
    },
    getLottery(type) {
      let vm = this;
      let params = {};
      params.token = localStorage.getItem("token");
      params.start_time = vm.beginTime;
      params.end_time = vm.endTime;
      params.status = vm.statusId;
      params.type = vm.lotteryType;
      params.page = vm.page;
      if (!type) {
        vm.$vux.loading.show();
      }
      vm.$http
        .post(vm.urlRequest + "?m=api&c=order&a=betList", params)
        .then((res) => {
          if (res.status == 0) {
            if (vm.lotteryList.length == 1) {
              vm.lotteryList = [{ id: 0, name: "全部彩种" }];
              vm.lotteryList = vm.lotteryList.concat(res.gameInfo);
              vm.statusList = [{ id: 0, name: "全部状态" }];
              vm.statusList = vm.statusList.concat(res.trantype);
              vm.lottery = vm.lotteryList[0];
              vm.status = vm.statusList[0];
              vm.lotteryType = vm.lotteryList[0].id;
              vm.statusId = vm.statusList[0].id;
            }
            if (vm.page == 1) {
              vm.recordList = res.list;
            } else {
              vm.recordList = vm.recordList.concat(res.list);
            }
            if (res.list.length < 20) {
              vm.flag = true;
            }
            vm.money = res.total.money;
            vm.award = res.total.award;
            vm.total = (+res.total.award - +res.total.money).toFixed(2);
          }
          vm.$vux.loading.hide();
        })
        .catch((err) => {
          vm.$vux.loading.hide();
          console.log(err);
        });
    },
    pickHide(data) {
      this.datePicker = data;
    },
    getTime(data) {
      let begin = new Date(data[0]);
      let end = new Date(data[1]);
      this.endTime = data[1];
      this.beginTime = data[0];
      this.datePicker = false;
      this.page = 1;
      this.getLottery();
    },
    picking(id) {
      if (this.type == 0) {
        this.lotteryType = id;
        this.lottery = this.lotteryList.find((item) => {
          return item.id == id;
        });
      } else if (this.type == 1) {
        this.statusId = id;
        this.status = this.statusList.find((item) => {
          return item.id == id;
        });
      }
      this.nowId = id;
      this.getLottery();
      let time = setTimeout(() => {
        this.pikerShow = false;
      }, 200);
    },
  },
  activated() {},
  deactivated() {},
  watch: {
    $route(to, from) {
      if (from.name != "TouZhuDetail" && to.name == "BettingRecord") {
        this.recordList = [];
        this.lotteryList = [{ id: 0, name: "全部状态" }];
        this.resetAll(1);
      }
    },
    lotteryList(list) {
      if (list.length) {
        for (let i = 0; i < list.length; i++) {
          if (list[i].id == this.$route.query.lottery_type) {
            this.lottery = list[i];
            this.lotteryType = list[i].id;
          }
        }
      }
    },
  },
};

// WEBPACK FOOTER //
// ./src/components/personalCenter/bettingRecord/bettingRecord.vue
